package com.dsg.service.account.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.dao.account.DsgAccountCashMapper;
import com.dsg.dao.account.DsgAccountLedgerMapper;
import com.dsg.dao.account.DsgAccountMapper;
import com.dsg.entity.account.DsgAccount;
import com.dsg.service.account.IDsgAccountCompareService;
import com.dsg.service.account.IDsgAccountService;
import com.dsg.util.RRException;
import com.dsg.util.SnowflakeKeyWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 油公司總帳 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-07-12
 */
@Service
public class DsgAccountServiceImpl extends ServiceImpl<DsgAccountMapper, DsgAccount> implements IDsgAccountService {

    @Autowired
    private DsgAccountMapper dsgAccountMapper;
    @Autowired
    private DsgAccountCashMapper dsgAccountCashMapper;
    @Autowired
    private DsgAccountLedgerMapper dsgAccountLedgerMapper;
    @Autowired
    private IDsgAccountCompareService dsgAccountCompareService;
    @Autowired
    private IDsgAccountService dsgAccountService;

    @Override
    public Page<DsgAccount> selectByList(Page<DsgAccount> page, DsgAccount dsgAccount) {
        return page.setRecords(dsgAccountMapper.selectByList(page, dsgAccount));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DsgAccount checkBank(DsgAccount dsgAccount) {
        DsgAccount da = getById(dsgAccount.getOid());
        if (da != null) {
//            da.setBankStaSum(dsgAccount.getBankStaSum());
//            da.setBankUseSum(dsgAccount.getBankUseSum());
//            da.setBankCheckTime(dsgAccount.getBankCheckTime());
//            if (da.getSysStaSum().compareTo(dsgAccount.getBankStaSum()) != 0 ||
//                    da.getSysUseSum().compareTo(dsgAccount.getBankUseSum()) != 0) {
//                return da;
//            } else {
//                updateById(da);
//            }
        } else {
            throw new RRException("賬戶匯總信息不存在！");
        }
        return null;
    }

    @Override
    public Page<DsgAccount> selectByListDetailed(Page<DsgAccount> page, DsgAccount dsgAccount) {
        return page.setRecords(dsgAccountMapper.selectByListDetailed(page, dsgAccount));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addDsgAccount(DsgAccount dsgAccount) {
        dsgAccount.setOid(UUIDGenerator.getUUID());
        dsgAccount.setTranCode(SnowflakeKeyWorker.nextStrId());
        dsgAccount.setIsCash(DsgAccount.IS_CASH_0);
        dsgAccount.setIsInSta(DsgAccount.IS_IN_STA_0);
        dsgAccount.setBalanceValue(BigDecimal.ZERO);
        dsgAccount.setRegStatus(DsgAccount.REG_STATUS_0);
        int i = dsgAccountMapper.insert(dsgAccount);
        return i > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void cash(String idList) {
        if (StringUtil.isEmpty(idList)) {
            throw new RRException("參數錯誤，OID不能為空！");
        }
        QueryWrapper<DsgAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("OID", Arrays.asList(idList.split(",")));
        List<DsgAccount> accountList = dsgAccountService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(accountList)) {
            QueryWrapper<DsgAccount> newQueryWrapper = new QueryWrapper<>();
            newQueryWrapper.eq("ACCOUNT_NO", accountList.get(0).getAccountNo());
            newQueryWrapper.eq("ENT_CODE", SingletonLoginUtils.getSystemEntCode());
            newQueryWrapper.eq("IS_CASH", DsgAccount.IS_CASH_1);
            newQueryWrapper.eq("REG_STATUS", DsgAccount.REG_STATUS_1);
            newQueryWrapper.orderByDesc("CREATE_TIME");
            newQueryWrapper.last("limit 1");
            DsgAccount newDsgAccount = dsgAccountService.getOne(newQueryWrapper);
            BigDecimal balanceValue = BigDecimal.ZERO;
            if (newDsgAccount != null) {
                balanceValue = newDsgAccount.getBalanceValue();
                newDsgAccount.setRegStatus(DsgAccount.REG_STATUS_0);
                dsgAccountService.updateById(newDsgAccount);
            }
            Date date = new Date();
            for (int i = 0; i < accountList.size(); i++) {
                DsgAccount dsgAccount = accountList.get(0);
                switch (dsgAccount.getInOut()) {
                    case "IN":
                        balanceValue = balanceValue.add(dsgAccount.getTraAmount());
                        break;
                    case "OUT":
                        balanceValue = balanceValue.subtract(dsgAccount.getTraAmount());
                        break;
                }
                dsgAccount.setIsCash(DsgAccount.IS_CASH_1);
                dsgAccount.setBalanceValue(balanceValue);
                dsgAccount.setCashTime(date);
                if (i == accountList.size() - 1) {
                    dsgAccount.setRegStatus(DsgAccount.REG_STATUS_1);
                }
            }
            dsgAccountService.updateBatchById(accountList);
        }
    }

}
